package com.example.dormmanage.handler;

import com.alibaba.fastjson.JSON;
import com.example.dormmanage.util.WebUtils;
import com.example.dormmanage.view.ResultView;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 登录失败处理器
 */
@Component
public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        String resultJson =
                JSON.toJSONString(ResultView.fail("用户认证失败请重新登录", HttpStatus.UNAUTHORIZED.value()));
        //处理异常
        WebUtils.renderString(response,resultJson);
    }
}
